//
//  SCPickerView.m
//  Shuaiche
//
//  Created by dangshuai on 2019/4/30.
//  Copyright © 2019 zzs. All rights reserved.
//

#import "SCPickerView.h"
#import "UIView+Line.h"
#import <Masonry.h>
@interface SCPickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIPickerView *pickerView;
@end

@implementation SCPickerView


- (instancetype)initWithDelegate:(id<SCPickerViewDelegate>)delegate {
    self = [super init];
    if (self) {
        self.backgroundColor = SC_RGBA(0, 0, 0, 0.5);
        _delegate = delegate;
        [self createSubview];
    }
    return self;
}

- (void)show {
    [self showWithView:nil];
}

- (void)showWithView:(UIView *)view {
    if (!view) {
        view = [UIApplication sharedApplication].keyWindow;
    }
    [view addSubview:self];
    self.frame = view.bounds;
    self.alpha = 0;
    [UIView animateWithDuration:.25 animations:^{
        self.alpha = 1;
    }];
}

- (void)hideView {
    [UIView animateWithDuration:.25 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)okButtonClick {
    NSInteger r = [_pickerView selectedRowInComponent:0];
    if (_picerDidSelect) {
        _picerDidSelect(r);
    } else {
        [_delegate pickerViewDidSelectedRow:r atCompont:0];
    }
    [self hideView];
}

- (void)createSubview {

    _contentView = [[UIView alloc] init];
    _contentView.backgroundColor = SC_Color_BG;
    [self addSubview:_contentView];
    [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(@0);
    }];
    
    UIPickerView *picker = [[UIPickerView alloc] init];
    picker.dataSource = self;
    picker.delegate = self;
    [_contentView addSubview:picker];
//    [picker addLineWithFrame:CGRectMake(0, 44, kSCScreenWidth, 0.5)];
//    [picker addLineWithFrame:CGRectMake(0, 88, kSCScreenWidth, 0.5)];
    [picker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_contentView.mas_left);
        make.right.equalTo(_contentView.mas_right);
        make.bottom.equalTo(_contentView.mas_bottom).offset(-kSCScreenBottomOffset);
        make.height.equalTo(@(5 * 44));
    }];
    _pickerView = picker;
    
    UIView *toolBar = [[UIView alloc] init];
    toolBar.backgroundColor = [UIColor whiteColor];
    [_contentView addSubview:toolBar];
    [toolBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_contentView.mas_left);
        make.right.equalTo(_contentView.mas_right);
        make.bottom.equalTo(picker.mas_top).offset(0);
        make.height.equalTo(@40);
        make.top.equalTo(_contentView.mas_top);
    }];
    
    UIButton *cancelButton  = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelButton.titleLabel.font = SC_FONT(14);
    cancelButton.frame = CGRectMake(0, 0, 46, 40);
    [cancelButton setTitleColor:SC_TextBlack_Primary forState:0];
    [cancelButton setTitle:@"取消" forState:0];
    [cancelButton addTarget:self action:@selector(hideView) forControlEvents:UIControlEventTouchUpInside];
    [toolBar addSubview:cancelButton];
    
    UIButton *selectButton  = [UIButton buttonWithType:UIButtonTypeCustom];
    selectButton.titleLabel.font = SC_FONT(14);
    selectButton.frame = CGRectMake(kSCScreenWidth - 46, 0, 46, 40);
    [selectButton setTitleColor:SC_TextYellow forState:0];
    [selectButton setTitle:@"确定" forState:0];
    [selectButton addTarget:self action:@selector(okButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [toolBar addSubview:selectButton];
    
    UIControl *control = [[UIControl alloc] init];
    [control addTarget:self action:@selector(hideView) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:control];
    [control mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(@0);
        make.bottom.equalTo(_contentView.mas_top);
    }];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    if ([_delegate respondsToSelector:@selector(numberOfComponentsInPickerView:)]) {
        return [_delegate numberOfComponentsInPickerView:pickerView];
    }
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return [_delegate pickerView:pickerView numberOfRowsInComponent:component];
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 44;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [_delegate pickerView:pickerView titleForRow:row forComponent:component];
}

- (void)setCurrentIndex:(NSInteger)currentIndex {
    _currentIndex = currentIndex;
    [_pickerView selectRow:currentIndex inComponent:0 animated:YES];
}

//- (nullable NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component {
//    NSString *string = [_delegate pickerView:pickerView titleForRow:row forComponent:component];
//    NSLog(@"string>>>%@,row>>>%zd",string,row);
//    NSAttributedString *attr = [[NSAttributedString alloc] initWithString:string attributes:@{NSFontAttributeName:SC_FONT(12),NSForegroundColorAttributeName:SC_TextBlack_Primary}];
//    return attr;
//}

- (UIView *)pickerView:(UIPickerView *)pickerView
            viewForRow:(NSInteger)row forComponent:(NSInteger)component
           reusingView:(UIView *)view{
    if (pickerView.subviews.count > 2) {
        [pickerView.subviews objectAtIndex:1].backgroundColor = SC_Color_Separator;
        [pickerView.subviews objectAtIndex:2].backgroundColor = SC_Color_Separator;
    }
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.numberOfLines = 1;
        pickerLabel.textColor = SC_TextBlack_Primary;
        pickerLabel.font = SC_FONT(13);
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
    }
    pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}

@end
